package MapRelated;

import java.util.ArrayList;
import java.util.List;

import ChatPackage.ChatHandler;
import Database.Database;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import androidApplication.javaFiles.ActionItem;
import androidApplication.javaFiles.QuickAction;
import androidApplication.javaFiles.R;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


public class UsersOverlay extends ItemizedOverlay<OverlayItem> {

	private List<OverlayItem> _items;
	private MapView _mapView;
	private Drawable _marker;
	private final String tag = new String("UsersOverlay");
	private QuickAction mQuickAction;
	private String _old_selected_device_id;
	private String _selected_device_id = new String("");
	private boolean _isMenuShowed = false;


	/*
	 * The snippet string is used for the device_id of the device behind this icon!
	 */
	public class MyOverlayItem extends OverlayItem 
	{

		public MyOverlayItem(GeoPoint point, String title, String snippet) 
		{  
			super(point, title, snippet);
		}


		@Override  
		public Drawable getMarker(int stateBitset) 
		{
			String device_id = getSnippet();

			if (_selected_device_id != null && _selected_device_id.equals(device_id)) //this icon is pressed!
			{  
				OverlayItem.setState(_marker, OverlayItem.ITEM_STATE_SELECTED_MASK);

				//the selected item was just changed, so we need to dismiss the previous menu (not relevant anymore)
				if(_old_selected_device_id != _selected_device_id & _isMenuShowed)
				{
					_isMenuShowed = false;
					mQuickAction.dismiss();
				}
				
				//if the menu is not shown, we should show it.
				if(!_isMenuShowed)
				{
					_old_selected_device_id = _selected_device_id;
					_isMenuShowed = true;
					showPopupMenu(_mapView, device_id);
				}

			} 
			else 
			{
				OverlayItem.setState(_marker, 0);
			}  
			
			return _marker;  
		}
	}


	public UsersOverlay(Drawable defaultMarker, MapView mv) 
	{
		super(boundCenterBottom(defaultMarker));
		this._mapView = mv;
		this._items = new ArrayList<OverlayItem>();
		this._marker = defaultMarker;
		initQuickActionMenu();

		//TODO - Delete this line!
		addFakeIcons();
	}


	@Override
	protected OverlayItem createItem(int index) 
	{
		return this._items.get(index);
	}


	@Override
	public int size() 
	{
		return this._items.size();
	}


	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.google.android.maps.ItemizedOverlay#draw(android.graphics.Canvas,
	 * com.google.android.maps.MapView, boolean)
	 */
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) 
	{
		super.draw(canvas, mapView, false); //the false cancels the shadow of the icon
		boundCenterBottom(this._marker);
	}


	public void addItem(OverlayItem item) 
	{	
		this._items.add(item);
		populate();
	}


	@Override
	public boolean onTap(int index)
	{
		Log.v(tag,"onTap!!!!!!!!!!");
		try
		{
			this._selected_device_id = this._items.get(index).getSnippet();
		}
		catch (Exception e) 
		{
			Log.v(tag,"onTap!! something went wrong!");
			e.printStackTrace();
		}

		return super.onTap(index);
	}


	private void initQuickActionMenu()
	{
		final Context context = this._mapView.getContext();

		//Chat action item
		ActionItem chatAction = new ActionItem();
		chatAction.setIcon(context.getResources().getDrawable(R.drawable.chat_icon));

		//Cancel action item
		ActionItem cancelAction = new ActionItem();
		cancelAction.setIcon(context.getResources().getDrawable(R.drawable.cancel_icon_blue));

		mQuickAction = new QuickAction(context);

		mQuickAction.addActionItem(chatAction);
		mQuickAction.addActionItem(cancelAction);
	}


	private void showPopupMenu(View view, final String device_id)
	{

		final Context context = _mapView.getContext();

		//setup the action item click listener
		mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() { 

			public void onItemClick(int pos) 
			{
				if (pos == 0) 
				{ 
					//Chat icon selected
					Log.v(tag, "Chat item selected");

					//the getSnippet of the item is the google account for now...
					ChatHandler.startChat(device_id, context);
				} 
				else if (pos == 1) 
				{ 
					//Cancel icon selected
					Log.v(tag, "Cancel button was pressed!");
					mQuickAction.dismiss();
				}
			}
		});

		mQuickAction.show(view);
	}


	public void addFakeIcons()
	{
		Log.v(tag,"adding Fake Icons");

		//adding 2 fake icons 
		String coordinates[] = {"31.644029", "34.956665"}; //coordinates of Israel
		GeoPoint point = MyMapView.getGeoPointFromCoordinate(coordinates);
		String device_id = Database.getInstance(this._mapView.getContext()).get_device_id();
		addItem(new MyOverlayItem(point, "Israel", device_id));

		//for now, the constant icons will be the user that uses this app

		coordinates[0] = "32.576100";
		coordinates[1] = "35";
		point = MyMapView.getGeoPointFromCoordinate(coordinates);
		addItem(new MyOverlayItem(point, "Israel", device_id + "t"));

		//for now, the constant icons will be the user that users this app

	}


	public void removeAllItems() 
	{
		this._items.clear();

		//TODO - delete!!!!!
		addFakeIcons();
	}

}